import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:zhrs/pages/login_shortmessage.dart';
import 'package:zhrs/utils/StaticContent.dart' ;
import 'login_fc.dart';
import '../api/api.dart';
import '../model/user_model.dart';
import '../utils/alert_utils.dart';
import '../utils/windowsSize.dart';
class LoginPage extends StatefulWidget {
LoginPage({Key key, this.title}) : super(key: key);
final String title;

@override
_LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool _phoneState, _pwdState = false;
  String _checkStr;
  TextEditingController _phonecontroller = new TextEditingController();
  TextEditingController _pwdcontroller = new TextEditingController();

  void _checkPhone() {
    if (_phonecontroller.text.isNotEmpty) {
      _phoneState = true;
    } else {
      _phoneState = false;
    }
  }

  void _checkPwd() {
    if (_pwdcontroller.text.isNotEmpty) {
      _pwdState = true;
    } else {
      _pwdState = false;
    }
  }
@override
  void initState() {
    // TODO: implement initState
    super.initState();

    StaticContent.contexts=context;
  }
  @override
  Widget build(BuildContext context)  {
    windowsSize.mapViewWidth=MediaQuery.of(context).size.width;
    windowsSize.mapViewHeight= MediaQuery.of(context).size.height;

    double mapViewWidth = MediaQuery.of(context).size.width;
    return  new Scaffold(

       /* appBar: new AppBar(
          title: new Text('极速登录'),
          backgroundColor: Colors.blue,
        ),*/
        body: new ListView(
          children: <Widget>[
            new Column(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                new Padding(
                    padding: new EdgeInsets.all(30.0),
                    child: new Image.asset(
                      'assets/images/login-logo.png',
                      scale: 1.2,
                      width: 70.0,
                    )),
                SizedBox(height: 60.0,),
                new Padding(
                  padding: new EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 15.0),
                  child: new Stack(
                    alignment: new Alignment(1.0, 1.0),
                    //statck
                    children: <Widget>[
                      new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            new Padding(
                              padding:
                              new EdgeInsets.fromLTRB(0.0, 0.0, 5.0, 0.0),
                              child: new Image.asset(
                                'assets/images/yhm.png',
                                width: 30.0,
                                height: 30.0,
                                fit: BoxFit.fill,
                              ),
                            ),
                            new Expanded(
                              child: new TextField(
                                controller: _phonecontroller,
                                keyboardType: TextInputType.phone,
                                decoration: new InputDecoration(
                                  hintText: '请输入用户名',
                                ),
                              ),
                            ),
                          ]),
                      new IconButton(
                        icon: new Icon(Icons.clear, color: Colors.black45),
                        onPressed: () {
                          _phonecontroller.clear();
                        },
                      ),
                    ],
                  ),
                ),
                new Padding(
                  padding: new EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 0.0),
                  child: new Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        new Padding(
                          padding: new EdgeInsets.fromLTRB(0.0, 0.0, 5.0, 0.0),
                          child: new Image.asset(
                            'assets/images/mm.png',
                            width: 30.0,
                            height: 30.0,
                            fit: BoxFit.fill,
                          ),
                        ),
                        new Expanded(
                          child: new TextField(
                            controller: _pwdcontroller,
                            decoration: new InputDecoration(
                              hintText: '请输入密码',
                              suffixIcon: new IconButton(
                                icon: new Icon(Icons.clear,
                                    color: Colors.black45),
                                onPressed: () {
                                  _pwdcontroller.clear();
                                },
                              ),
                            ),
                            obscureText: true,
                          ),
                        ),
                      ]),
                ),
               Container(
                 height: 25,
                 width:mapViewWidth-1 ,
                 padding: EdgeInsets.fromLTRB(mapViewWidth-100,0,0,0),
                  child: FlatButton(
                    onPressed: (){},
                       child: new Text(
                              '忘记密码？',
                              style: new TextStyle(
                               color: Colors.blue, fontSize: 13.0,),
                                       ),
                                   ),
                                     ),
                new Container(
                  padding: EdgeInsets.fromLTRB(0,20,0,0),
                  width: 420.0,
                  child: new Card(
                    color: Colors.blue,
                    elevation: 0.0,
                    child: new FlatButton(
                      child: new Padding(
                        padding: new EdgeInsets.all(10.0),
                        child: new Text(
                          '登录',
                          style: new TextStyle(
                              color: Colors.white, fontSize: 16.0),
                        ),
                      ),
                      onPressed: () {
                        _checkPhone();
                        _checkPwd();
                        if (_phoneState && _pwdState) {

                          HttpManager.login(context, UserModel(loginname: _phonecontroller.text.trim(),password: _pwdcontroller.text.trim()),true);
                        } else {
                          if (!_phoneState) {
                            _checkStr = '请输入用户名！';
                          } else if (!_pwdState) {
                            _checkStr = '请输入密码！';
                          }
                        }
                        if(_checkStr!=null) {
                          alertDialog(context, _checkStr);
                          print(_checkStr);
                        }
                      },
                    ),
                  ),
                ),
                buildForgetPasswordText(context),
                buildTitle(),
                buildOtherLoginText(),
                new Padding(
                  padding: new EdgeInsets.fromLTRB(5.0, 20.0, 5.0, 5.0),
                  child: new Stack(
                    alignment: new Alignment(1.0, 1.0),
                    //statck
                    children: <Widget>[
                      new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            new Padding(
                              padding:
                              new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                              child: new Image.asset(
                                'assets/images/my-login-link-ico1.png',
                                width: 35.0,
                                height: 35.0,
                                fit: BoxFit.fill,
                              ),
                            ),
                            new Padding(
                              padding:
                              new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                              child: new Image.asset(
                                'assets/images/my-login-link-ico2.png',
                                width: 35.0,
                                height: 35.0,
                                fit: BoxFit.fill,
                              ),
                            ),
                            new Padding(
                              padding:
                              new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                              child: new Image.asset(
                                'assets/images/my-login-link-ico3.png',
                                width: 35.0,
                                height: 35.0,
                                fit: BoxFit.fill,
                              ),
                            ),
                            new Padding(
                              padding:
                              new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                              child: new Image.asset(
                                'assets/images/my-login-link-ico4.png',
                                width: 35.0,
                                height: 35.0,
                                fit: BoxFit.fill,
                              ),
                            ),
                            new Padding(
                              padding:
                              new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                              child: new Image.asset(
                                'assets/images/my-login-link-ico5.png',
                                width: 35.0,
                                height: 35.0,
                                fit: BoxFit.fill,
                              ),
                            ),
                          ]),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      );
  }
}
Align buildOtherLoginText() {

  return Align(
      alignment: Alignment.center,
      child: Text(
        '—————其他账号登录—————',
        style: TextStyle(color: Colors.grey, fontSize: 15.0),
      ));
}
Padding buildTitle() {
  return Padding(
    padding: EdgeInsets.all(30.0),
  );
}
Padding buildForgetPasswordText(BuildContext context) {
  return Padding(
    padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
    child: new Stack(
      alignment: new Alignment(1.0, 1.0),
      //statck
      children: <Widget>[
        new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              new Padding(
                padding: new EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 0.0),
                child: Align(
                  alignment: Alignment.topLeft,
                  child: FlatButton(
                    child: Text(
                      '注册账号',
                      style: TextStyle(fontSize: 14.0, color: Colors.blue),
                    ),
                    onPressed: () {
                     // Navigator.pop(context);
                    },
                  ),
                ),
              ),
              new Padding(
                padding: new EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
                child: Align(
                  alignment: Alignment.topRight,
                  child: FlatButton(
                    child: Text(
                      '短信验证码登录',
                      style: TextStyle(fontSize: 14.0, color: Colors.blue),
                    ),
                    onPressed: () {
                      //
                      Navigator.push(context, MaterialPageRoute(builder: (context){
                        return Login_shortmessage();
                      }));
                    },
                  ),
                ),
              ),
              new Padding(
                padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                child: Align(
                  alignment: Alignment.topLeft,
                  child: FlatButton(
                    child: Text(
                      ' 人脸识别登录',
                      style: TextStyle(fontSize: 14.0, color: Colors.blue),
                    ),
                    onPressed: () {
                    Navigator.push(context, MaterialPageRoute(builder: (context){
                      return LoginFcPage();
                    }));
                    },
                  ),
                ),
              ),
            ]),
      ],
    ),
  );
}